﻿using System;
using System.Linq;
using DataProcessor.EF;
using Hjt212Parser;
using log4net;


namespace DataProcessor.Process
{
    public class ProcessorMonitor //: IProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProcessorMonitor));

        //实时数据处理
        public static void Process(string content)
        {
            try
            {
                using (var ctx = new PlatformXpfContext())
                {
                    var mn = Parser.GetStringByKey("MN", content);
                    if (mn == null || mn.Length < 14)
                    {
                        Log.Warn("MN is null.");
                        return;
                    }
                    var info = Util.GetInfoFromMn(mn);
                    if (info == null)
                    {
                        Log.Warn("MN：" + mn + " 找不到对应企业!");
                        return;
                    }
                    var dataTime = Parser.GetDateTimeByKey("DataTime", content);
                    if (!dataTime.HasValue)
                    {
                        Log.Warn("DataTime is null.");
                        return;
                    }
                    var pollutantCodes = Parser.GetPollutantCodes(content);
                    if (pollutantCodes==null|| pollutantCodes.Count<1)
                    {
                        Log.Warn("Pollutant Codes is null.");
                        return;
                    }
                    var masterCode = Util.GetMasterPollutionCode(pollutantCodes);
                    if (masterCode == null)
                    {
                        //Log.Warn("Master Pollutant Code is null.");
                        //return;
                        masterCode = "B01";
                    }
                    var pollutionInfo = Util.GetContaminantByCode(masterCode);
                    if (pollutionInfo == null)
                    {
                        Log.Warn("Platform Contaminant is null.");
                        return;
                    }

                    var masterMonitor = ctx.主实时监控.Where(t => t.企业基本信息ID == info.企业基本信息ID).FirstOrDefault(t => t.企业排口ID == info.ID);
                    if (masterMonitor == null)
                    {
                        masterMonitor = new 主实时监控
                        {
                            企业基本信息ID = info.企业基本信息ID,
                            企业排口ID = info.ID,
                            污染物类别ID = pollutionInfo.污染物类别ID,
                            实时数据时间 = null,
                            分钟数据时间 = null,
                            小时数据时间 = null,
                            日数据时间 = null
                        };
                        ctx.主实时监控.Add(masterMonitor);
                        ctx.SaveChanges();
                    }
                    var cn = Parser.GetStringByKey("CN", content);

                    switch (cn)
                    {
                        case "2011":
                        case "G011":
                            if (masterMonitor.实时数据时间.HasValue && masterMonitor.实时数据时间.Value > dataTime.Value)
                            {
                                Log.Warn("无须处理：监控表时间新" + masterMonitor.实时数据时间.Value + "：\r\n" + content);
                                return;
                            }
                            masterMonitor.实时数据时间 = dataTime;
                            var rtdDouble = Parser.GetDoubleByKey(masterCode + "-Rtd", content);
                            //if (rtdDouble == null)
                            //{
                            //    Log.Warn(masterCode + "-Rtd is null.");
                            //    return;
                            //}
                            masterMonitor.瞬时量 = rtdDouble;
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo == null)
                                {
                                    Log.Warn("Platform Contaminant:"+ detailCode+" is null.");
                                    continue;
                                }

                                rtdDouble = Parser.GetDoubleByKey(detailCode + "-Rtd", content);
                                if (rtdDouble == null)
                                {
                                    Log.Warn(detailCode + "-Rtd" + " is null.");
                                    continue;
                                }
                                var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitor == null)
                                {
                                    detailMonitor = new 从实时监控 { 污染物编码ID = pollutionInfo.ID };
                                    masterMonitor.从实时监控.Add(detailMonitor);
                                }
                                detailMonitor.瞬时量 = rtdDouble.Value;
                                detailMonitor.折算瞬时量 = Parser.GetDoubleByKey(detailCode + "-ZsRtd", content);
                            }
                            break;
                        case "2051":
                        case "G051":
                            if (masterMonitor.分钟数据时间.HasValue && masterMonitor.分钟数据时间.Value > dataTime.Value)
                            {
                                Log.Warn("无须处理：监控表时间新" + masterMonitor.分钟数据时间.Value + "：\r\n" + content);
                                return;
                            }
                            masterMonitor.分钟数据时间 = dataTime;
                            masterMonitor.分钟排放量 = Parser.GetDoubleByKey(masterCode + "-Cou", content);
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo==null) continue;
                                var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitor == null)
                                {
                                    detailMonitor = new 从实时监控 { 污染物编码ID = pollutionInfo.ID };
                                    masterMonitor.从实时监控.Add(detailMonitor);
                                }
                                detailMonitor.分钟排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                            }
                            break;
                        case "2061":
                        case "G061":
                            if (masterMonitor.小时数据时间.HasValue && masterMonitor.小时数据时间.Value > dataTime.Value)
                                return;
                            masterMonitor.小时数据时间 = dataTime;
                            masterMonitor.小时排放量 = Parser.GetDoubleByKey(masterCode + "-Cou", content);
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo==null) continue;
                                var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitor == null)
                                {
                                    detailMonitor = new 从实时监控 { 污染物编码ID = pollutionInfo.ID };
                                    masterMonitor.从实时监控.Add(detailMonitor);
                                }
                                detailMonitor.小时排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                            }
                            break;
                        case "2031":
                        case "G031":
                            if (masterMonitor.日数据时间.HasValue && masterMonitor.日数据时间.Value > dataTime.Value)
                                return;
                            masterMonitor.日数据时间 = dataTime;
                            masterMonitor.日排放量 = Parser.GetDoubleByKey(masterCode + "-Cou", content);
                            foreach (var detailCode in pollutantCodes)
                            {
                                if (detailCode.Equals(masterCode))
                                {
                                    continue;
                                }
                                pollutionInfo = Util.GetContaminantByCode(detailCode);
                                if (pollutionInfo==null) continue;

                                var detailMonitor = masterMonitor.从实时监控.FirstOrDefault(t => t.污染物编码ID == pollutionInfo.ID);
                                if (detailMonitor == null)
                                {
                                    detailMonitor = new 从实时监控 { 污染物编码ID = pollutionInfo.ID };
                                    masterMonitor.从实时监控.Add(detailMonitor);
                                }
                                detailMonitor.日排放量 = Parser.GetDoubleByKey(detailCode + "-Cou", content);
                            }
                            break;
                        default: return;
                    }
                    ctx.SaveChanges();
                    Log.Info("同步Monitor成功。" );
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex+Environment.NewLine+content);
            }
        }
    }
}
